package com.thingsgrid.notice.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.thingsgrid.notice.repository.ICommonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
/**
 * CommonRepositoryImpl
 * @author MQ
 */
@Repository
public class CommonRepositoryImpl implements ICommonRepository{

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public boolean updateByName(Class class1, String queryName, String queryVal, String updateName, String updateVal) {
        Query query = new Query(Criteria.where(queryName).is(queryVal));
        Update update= new Update().set(updateName, updateVal);
        long count = mongoTemplate.updateFirst(query,update,class1).getModifiedCount();
        if(count>0) {
            return true;
        }else {
            return false;
        }

    }
    @Override
    public boolean batchDeleteByName(Class class1,List<String> ids,String name) {
        Query query = new Query(Criteria.where(name).in(ids));
        long count = mongoTemplate.remove(query, class1).getDeletedCount();
        if(count>0) {
            return true;
        }else {
            return false;
        }

    }

    @Override
    public <T>List<T> queryLikeByName(T t,String querynName, String queryVal) {
        Query query = new Query();
        query.addCriteria(Criteria.where(querynName).regex(".*" +queryVal.toString()+ ".*"));
        return mongoTemplate.find(query, (Class<T>) t.getClass());
    }

    @Override
    public <T> List<T> queryLikeByName(T t, Criteria criatira) {
        Query query = new Query(criatira);
        return mongoTemplate.find(query, (Class<T>) t.getClass());
    }

    @Override
    public <T> List<T> queryByNames(T t,String queryName ,List<String> queryVals) {
        Query querys = new Query();
        querys.addCriteria(Criteria.where(queryName).in(queryVals));
        return mongoTemplate.find(querys, (Class<T>)t.getClass());
    }
    @Override
    public <T> List<T> queryByName(T t,String queryName ,String queryVal) {
        Query querys = new Query();
        querys.addCriteria(Criteria.where(queryName).is(queryVal));
        return mongoTemplate.find(querys, (Class<T>)t.getClass());
    }
    @Override
    public boolean updateBatch(Class class1, String queryName, List<String> queryVals, List<JSONObject> updateNames){
        Query updateQuerys = new Query();
        updateQuerys.addCriteria(Criteria.where(queryName).in(queryVals));
        Update update = new Update();
        updateNames.forEach(v->v.keySet().forEach(n->update.set(n,v.get(n))));
        long count = mongoTemplate.updateMulti(updateQuerys, update, class1).getModifiedCount();
        if(count>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateBatch(Class class1, String queryName, String queryVal, Map map) {
        Query queryOne = new Query();
        queryOne.addCriteria(Criteria.where(queryName).is(queryVal));
        List list = mongoTemplate.find(queryOne,class1);
        Query updateQuery = new Query();
        updateQuery.addCriteria(Criteria.where(queryName).is(queryVal));
        Update update = new Update();
        list.forEach(v->map.keySet().forEach(n->update.set(n.toString(),map.get(n))));
        long count = mongoTemplate.updateMulti(updateQuery, update, class1).getModifiedCount();
        if(count>0) {
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean updateBatch(Class class1, String queryName, List<String> queryVals, String updateName, String updateVal) {
        Query updateQuerys = new Query();
        updateQuerys.addCriteria(Criteria.where(queryName).is(queryVals));
        Update update = new Update();
        queryVals.forEach(v->update.set(updateName,updateVal));
        long count = mongoTemplate.updateMulti(updateQuerys, update,class1).getModifiedCount();
        if(count>0) {
            return true;
        }else {
            return false;
        }
    }

}
